package com.leroymerlin.commit;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vcs.CommitMessageI;
import com.intellij.openapi.vcs.VcsDataKeys;
import com.intellij.openapi.vcs.ui.Refreshable;
import org.jetbrains.annotations.Nullable;

/**
 * @author Damien Arrachequesne
 */
public class CreateCommitAction extends AnAction implements DumbAware {

    @Override
    public void actionPerformed(AnActionEvent actionEvent) {
        final CommitMessageI commitPanel = getCommitPanel(actionEvent);
        if (commitPanel == null)
            return;

//        CommitDialog dialog = new CommitDialog(actionEvent.getProject());
//        dialog.show();
//        if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
//            commitPanel.setCommitMessage(dialog.getCommitMessage().toString());
//        }

        // 1. 第一期先简单发送，后续改界面，加入界面选择导入
        StringBuilder builder = new StringBuilder();
        builder.append("[feature/bugfix/others][null][platform][project][WHAT]");
        builder.append(System.lineSeparator());
        builder.append(System.lineSeparator());
        builder.append("[WHY]");
        builder.append(System.lineSeparator());
        builder.append("[HOW]");
        builder.append(System.lineSeparator());
        builder.append("[测试建议]");
        builder.append(System.lineSeparator());
        builder.append("1.");
        builder.append(System.lineSeparator());
        builder.append("[自测结果]");
        builder.append(System.lineSeparator());
        builder.append("1.");

        commitPanel.setCommitMessage(builder.toString());

    }

    @Nullable
    private static CommitMessageI getCommitPanel(@Nullable AnActionEvent e) {
        if (e == null) {
            return null;
        }
        Refreshable data = Refreshable.PANEL_KEY.getData(e.getDataContext());
        if (data instanceof CommitMessageI) {
            return (CommitMessageI) data;
        }
        return VcsDataKeys.COMMIT_MESSAGE_CONTROL.getData(e.getDataContext());
    }
}
